//
//  Random.swift
//  数据结构
//
//  Created by XFB on 2020/4/3.
//  Copyright © 2020 XFB. All rights reserved.
//  生成随机数

import Foundation

// 随机数数组
func RandomArray(min: Int, max: Int, length: Int) -> [Int] {
    if length <= 0 || min > max {
        return []
    }
        
    let random = max - min + 1
    var array : [Int] = []
    for _ in 0 ..< length {
        // 生成随机数
//        array.append( Int(arc4random()%99999) + 1)
        array.append( Int(arc4random()) % random + min)
    }
    return array
}

// 升序数组
func ascOrderRandomArray(min: Int, max: Int) -> [Int] {
    if min > max {
        return []
    }
    var array : [Int] = []
    for i in min ... max {
        array.append(i)
    }
    return array
}

// 降序数组
func descOrderRandomArray(min: Int, max: Int) -> [Int] {
    if min > max {
        return []
    }
    var array : [Int] = []
    for i in (min ... max).reversed() {
        array.append(i)
    }
    return array
}

// 尾巴是排好序，前面是随机数
func tailOrderRandomArray(min: Int, max: Int, orderCount: Int) -> [Int] {
    // 首先生成随机串
    let array = RandomArray(min: min, max: max, length: max-min+1)
    if orderCount > array.count  {
        return array
    }
    let start = max-min-orderCount + 1
    let end = max-min
    return reverse(nums: array, start: start, end: end)
}

func reverse(nums: [Int], start: Int, end: Int) -> [Int] {
    var array = nums
    let subArray = nums[start...end]
    array.replaceSubrange(start...end, with: subArray.sorted())
    return array
}


// 是否是排序好
func isAscOrder(array: [Int]) -> Bool {
    
    guard array.count > 0 else {
        return false
    }
    
//    if array.count == 0 {
//        return false
//    }
    for i in 1 ..< array.count {
        if array[i-1] > array[i] {
            return false
        }
    }
    return true
}


